﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using vngoodlife.Util;
using vngoodlife.Singleton;

namespace vngoodlife.Controllers
{
    public class MenuListController : Controller
    {

        #region Backend

        [CheckMemberVisibility, Authorize(Roles = ConstantClass.superadmin)]
        public ActionResult MenuList_BackEnd(String flag, String menuListID)
        {

            string menuTypeID = "";
            if (Request.Cookies[ConstantClass.cooMenuType] != null && Request.Cookies[ConstantClass.cooMenuType].Value != "")
            {
                menuTypeID = Request.Cookies[ConstantClass.cooMenuType].Value;
            }

            //ViewData for MenuList
            ViewBag.MenuTypeList = new SelectList(DataSingleton.Instance.GetMenuListModels().GetMenuType().ToList(), "MenuTypeID", "MenuTypeName", menuTypeID);

            ViewBag.MenuListAction = flag;

            if (flag == "Edit")
            {
                ViewBag.MenuListEditMenuListID = menuListID;
            }

            return View("/Views/BackEnd/MenuList/MenuList.cshtml", DataSingleton.Instance.GetMenuListModels().GetMenuListView(menuTypeID));
            //return View("/Views/BackEnd/MenuList/MenuList.cshtml", null);
        }


        [ChildActionOnly, CheckMemberVisibility, Authorize]
        public ActionResult BackendMenuList()
        {
            return View("/Views/Shared/Backend/_BackendMenu.cshtml", DataSingleton.Instance.GetMenuListModels().GetBackEndMenuList());
        }

        [CheckMemberVisibility, Authorize, HttpPost, ValidateAntiForgeryToken(Salt = AntiKey.EditSalt)]
        public ActionResult ChangeMenuType(String menuTypeID)
        {
            if (Request.Cookies[ConstantClass.cooMenuType] == null)
            {
                HttpCookie cookie = new HttpCookie(ConstantClass.cooMenuType, menuTypeID);
                Response.Cookies.Add(cookie);
                cookie.Expires = DateTime.Now.AddDays(ConstantClass.cooMenuTypeTimeLife);
            }
            else
            {
                Response.Cookies[ConstantClass.cooMenuType].Value = menuTypeID;
            }

            return RedirectToAction("MenuList_BackEnd", "MenuList");
        }

        [ChildActionOnly, CheckMemberVisibility, Authorize(Roles = ConstantClass.menuAuthentication)]
        public ActionResult MenuListCreate()
        {
            ViewBag.DDLMenuList = new SelectList(DataSingleton.Instance.GetMenuListModels().GetMenuListForDDL(), "MenuListID", "MenuName");
            ViewBag.MenuTypeListCreate = new SelectList(DataSingleton.Instance.GetMenuListModels().GetMenuType().ToList(), "MenuTypeID", "MenuTypeName");
            return View("/Views/BackEnd/MenuList/_MenuListCreate.cshtml");
        }

        [CheckMemberVisibility]
        [Authorize(Roles = ConstantClass.menuAuthentication), HttpPost, ValidateInput(false), ValidateAntiForgeryToken(Salt = AntiKey.EditSalt)]
        public ActionResult MenuListCreateSubmit(HttpPostedFileBase uploadFile)
        {
            string username = HttpContext.User.Identity.Name;
            MenuList menuList = new MenuList();
            string newMenuImage = "";

            //If upload image
            if (uploadFile != null && uploadFile.ContentLength > 0 && TryUpdateModel(menuList))
            {
                //Upload Image of Menu
                newMenuImage = DataSingleton.Instance.GetGeneralModels().MenuListUploadImage(uploadFile, Request.PhysicalApplicationPath.ToString(), 
                    menuList.MenuName , "");

                DataSingleton.Instance.GetMenuListModels().MenuListCreate(menuList, username, newMenuImage);
            }
            //If not upload image
            else if (ModelState.IsValid && TryUpdateModel(menuList))
            {
                DataSingleton.Instance.GetMenuListModels().MenuListCreate(menuList, username, newMenuImage);
            }
            else
            {
                ModelState.AddModelError("NoticePage", "Có lối khi tạo menulist");
            }

            ViewBag.DDLMenuList = new SelectList(DataSingleton.Instance.GetMenuListModels().GetMenuListForDDL(), "MenuListID", "MenuName");
            ViewBag.MenuTypeListCreate = new SelectList(DataSingleton.Instance.GetMenuListModels().GetMenuType().ToList(), "MenuTypeID", "MenuTypeName");

            return RedirectToAction("MenuList_BackEnd", "MenuList", new { flag = "Create", menuListID = "" });
        }


        [ChildActionOnly, CheckMemberVisibility, Authorize(Roles = ConstantClass.menuAuthentication)]
        public ActionResult MenuListEdit(int menuListID)
        {

            //Get information of a menu and pass to partial MenuListEdit
            MenuList menuList = DataSingleton.Instance.GetMenuListModels().GetMenuByID(menuListID);

            //ViewData For MenuListEdit
            ViewBag.MenuTypeListEdit = new SelectList(DataSingleton.Instance.GetMenuListModels().GetMenuType().ToList(), "MenuTypeID", "MenuTypeName");
            ViewBag.DDLMenuList = new SelectList(DataSingleton.Instance.GetMenuListModels().GetMenuListForDDL(), "MenuListID", "MenuName");


            return View("/Views/BackEnd/MenuList/_MenuListEdit.cshtml", menuList);
        }

        [CheckMemberVisibility]
        [Authorize(Roles = ConstantClass.menuAuthentication), HttpPost, ValidateInput(false), ValidateAntiForgeryToken(Salt = AntiKey.EditSalt)]
        public ActionResult MenuListEditSubmit(MenuList menuList, HttpPostedFileBase uploadFile, String oldImage)
        {

            string newMenuImage = "";

            //if (ModelState.IsValid)
            //{
                //If upload image
                if (uploadFile != null && uploadFile.ContentLength > 0)
                {
                    //Upload Image of Menu
                    newMenuImage = DataSingleton.Instance.GetGeneralModels().MenuListUploadImage(uploadFile, Request.PhysicalApplicationPath.ToString(),
                        menuList.MenuName, oldImage);
                }

                DataSingleton.Instance.GetMenuListModels().EditMenuList(menuList, newMenuImage);
            //}

            ViewBag.MenuTypeListEdit = new SelectList(DataSingleton.Instance.GetMenuListModels().GetMenuType().ToList(), "MenuTypeID", "MenuTypeName");
            ViewBag.DDLMenuList = new SelectList(DataSingleton.Instance.GetMenuListModels().GetMenuListForDDL(), "MenuListID", "MenuName");

            return RedirectToAction("MenuList_BackEnd", "MenuList", new { flag = "Edit", menuListID = menuList.MenuListID });
        }



        /// <summary>
        /// Delete a Menu
        /// </summary>
        /// <param name="menuListID"></param>
        /// <returns></returns>
        [CheckMemberVisibility, Authorize(Roles = ConstantClass.menuAuthentication), HttpPost, ValidateAntiForgeryToken(Salt = AntiKey.EditSalt)]
        public ActionResult MenuListDelete(int menuListID)
        {
            DataSingleton.Instance.GetMenuListModels().DeleteMenuList(menuListID, (Request.PhysicalApplicationPath.ToString() + "Content/images/backend/"));
            return RedirectToAction("MenuList_BackEnd", "MenuList");
        }


        public string CheckAdminRoleInMenu(int menuListID)
        {

            IQueryable<Role_MenuList> roleMenu = DataSingleton.Instance.GetLinQContext().Role_MenuLists.Where(m => m.MenuListID == menuListID);

            bool isAllow = false;
            foreach (var item in roleMenu)
            {
                if (HttpContext.User.IsInRole(item.RoleID))
                {
                    isAllow = true;
                    break;
                }
            }

            return isAllow.ToString();
        }



        #endregion




        #region Frontend


        [ChildActionOnly]
        public ActionResult FrontendMenuList()
        {
            return View("/Views/Shared/FrontEnd/_MainMenu.cshtml", DataSingleton.Instance.GetMenuListModels().GetFrontEndMenuList());
        }


        [ChildActionOnly]
        public ActionResult FrontEndSubMenuList(int menuListID)
        {
            IQueryable<MenuList> subMenuList = DataSingleton.Instance.GetMenuListModels().GetFrontEndSubMenuList(menuListID);

            if (subMenuList.Count() > 0)
            {
                return View("/Views/Shared/FrontEnd/_SubMainMenu.cshtml", subMenuList);
            }
            else
            {
                return View("/Views/Shared/FrontEnd/_SubMainMenu.cshtml", null);
            }
        }


        [ChildActionOnly]
        public ActionResult MenuSiteMap(int menuListID = 0)
        {
            return View("/Views/FrontEnd/Article/_SiteMap.cshtml", DataSingleton.Instance.GetMenuListModels().GetMenuSiteMap(menuListID) );
        }


        public string GetMenuSeoUrl(int menuListID)
        {
            return DataSingleton.Instance.GetMenuListModels().GetMenuSeoUrl(menuListID) ;
        }





        #endregion

    }
}
